﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Net;
using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Windows.Media.Imaging;

namespace AzukiMapUI
{
	public partial class IconSelector : UserControl
	{
		public IconSelector()
		{
			// Required to initialize variables
			InitializeComponent();
            SetIcons();
		}

        void SetItems()
        {
        }

        void SetIcons()
        {
            string uriStr = "http://localhost/AzukiMap/getIcons.php";

            WebClient webClient = new WebClient();
            webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
            webClient.DownloadStringAsync(new Uri(uriStr));
        }

        void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                string res = e.Result;
                AddIcons(res);
            }
            else
            {
            }
        }

        void AddIcons(string resText)
        {
            XDocument iconsXml = XDocument.Parse(resText);
            IEnumerable<SelectableElement> sources = from elem in iconsXml.Descendants("icon")
                         select new SelectableElement
                         { 
                             SouceUri = new Uri("http://localhost/AzukiMap/" + (string)elem.Attribute("file")),
                             Name = (string)elem.Attribute("name"),
                             Size = (int)elem.Attribute("size"),
                             Width = (int)elem.Attribute("width"),
                             Height = (int)elem.Attribute("height")
                         };
            foreach (SelectableElement source in sources)
            {
                Image icon = CreateImage(source.SouceUri);
                icon.Height = source.Height;
                icon.Width = source.Width;
                icon.Margin = new Thickness(4);
                ItemContainer.Items.Add(icon);
            }
        }

        Image CreateImage(Uri imageUri)
        {
            Image img = new Image();
            BitmapImage imgSource = new BitmapImage(imageUri);
            img.Source = imgSource;
            return img;
        }
	}

    public class SelectableElement
    {
        Uri _souceUri;
        string _name;
        int _size;
        int _width;
        int _height;

        public Uri SouceUri
        {
            get { return _souceUri; }
            set { _souceUri = value; }
        }

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public int Size
        {
            get { return _size; }
            set { _size = value; }
        }

        public int Width
        {
            get { return _width; }
            set { _width = value; }
        }

        public int Height
        {
            get { return _height; }
            set { _height = value; }
        }

        
        public SelectableElement()
        {
        }
    }
}